/**
 * Exports part (require)
 */
var express = require('express');
var flash = require('connect-flash');
var passport = require('passport');
var path = require('path');
var mongoose = require('mongoose');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var app = express();
var dbUrl = require('./config/databaseconnection');
var http = require('http');

/**
 * Local variables
 */
var port = process.env.port || 3000;


/**
 * Mongo db connection
 */
mongoose.connect(dbUrl.local);

require('./config/passport')(passport); // pass passport for configuration
require('./routes/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport

/**
 * App configuration
 */
app.configure(function() {

    // set up our express application
    app.use(express.logger('dev')); // log every request to the console
    app.use(express.cookieParser()); // read cookies (needed for auth)
    app.use(express.bodyParser()); // get information from html forms
    app.use(express.json());
    app.use(express.urlencoded());
    app.use(express.errorHandler());

    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'ejs'); // set up ejs for templating
    app.set('port', process.env.PORT || 3000);

    // required for passport
    app.use(express.session({ secret: '123456' })); // session secret
    app.use(passport.initialize());
    app.use(passport.session()); // persistent login sessions
    app.use(flash()); // use connect-flash for flash messages stored in session

});


/**
 * Run api server
 */
http.createServer(app).listen(app.get('port'), function(){
    console.log('Listening on port ' + app.get('port'));
});
